
public class Main {
	/*
	 * Xn = Xn-1 - f(Xn)/f'(Xn)
	 * 
	 * */
	final static double e = 1e-5;
	final static int MAXITERATIONS = 5000;
	public static void main(String[] args) {
	System.out.println("Початок ...");
	double X1 = Double.MIN_NORMAL,X0 = 1;
	//algorithm
	int i = 1;
	while(Math.abs(X1 - X0) > e  && i < MAXITERATIONS)
	{
		double prev = X0;
		X1 = X0 - func(X0)/derivativeFunc(X0);
		System.out.println(String.format("[%d]X1 = %.8f, X0 = %.8f, func = %.8f, derivative = %.8f",i,X1, X0,func(X0),derivativeFunc(X0)));
		X0 = X1;
		X1 = prev;
		System.out.println("New X0 = "+X0);
		i++;		
	}
	System.out.println(String.format("x = %.5f", X1));
	//end		
	}
	
	/**
	 *  f(x) = cos x - x2 - 2; 
	 */
	public static double func(double x)
	{
		return Math.cos(x) - Math.pow(x, 2) - 2;
	}
	
	/**
	 * f'(x) = -2x - sin(x)
	 */
	public static double derivativeFunc(double x)
	{
		return  -2*x-Math.sin(x);
	}

}
